﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.RemoteMachine
{
    public class HubConnection : Connection
    {
        public HubConnection(Hub hub) : base(hub)
        {

        }

        /// <summary>
        /// Called after the connection to the Hub is established
        /// </summary>
       /* protected override void OnConnect()
        {
            // Log
            //log.Info("[" + this.ToString() + "] Connected. ");

            // Call ProtocolFeature handlers
            foreach (Protocol.ProtocolFeature f in _parentNode.Features.Values)
            {
                // Handle Connect
                f.HandleConnect(this._parentNode);
            }

            // Raise Event
            //RaiseConnected(new NodeEventArgs());
        }*/

        /// <summary>
        /// Called after the connection to the Hub is terminated
        /// </summary>
        protected override void OnDisconnect()
        {
            // Log
            //log.Debug("Disconnected.");

            // Call ProtoclFeature handlers
            foreach (Network.Protocol.ProtocolFeature f in mParentNode.Features.Values)
            {
                // Handle Disconnect
                f.HandleDisconnect(this.mParentNode);
            }

            // Raise Event
            //RaiseDisconnected(new NodeEventArgs());
        }

        /// <summary>
        /// Called after a message is received from the Hub
        /// </summary>
        protected override void OnMessageReceive(Network.Protocol.ProtocolMessage message)
        {
            // Log
            //log.Debug("[" + this.ToString() + "] Message Received: " + protocolMessage.ToString());
            //System.Diagnostics.Debug.WriteLine(message.ToString());

            List<Network.Protocol.ProtocolFeature> featuresSupportingMessage = new List<Netfraction.Network.Protocol.ProtocolFeature>();
            List<Network.Protocol.ProtocolFeature> featuresSupportingMessageExtension = new List<Netfraction.Network.Protocol.ProtocolFeature>();
            foreach (Network.Protocol.ProtocolFeature f in mParentNode.Features.Values)
            {
                if (f.SupportsActionOfType(message.MessageCode, Netfraction.Network.Protocol.FeatureActionType.Message))
                    featuresSupportingMessage.Add(f);
                else if (f.SupportsActionOfType(message.MessageCode, Netfraction.Network.Protocol.FeatureActionType.MessageExtension))
                    featuresSupportingMessageExtension.Add(f);
            }
            /*from p in _parentNode.Features.Values
                                            where p.SupportsActionOfType(message.MessageCode, Netfraction.Network.Protocol.FeatureActionType.Message)
                                            select p;*/
            //System.Diagnostics.Debug.WriteLine(featuresSupportingMessage.Count());
            /*foreach (Network.Protocol.ProtocolFeature feat in featuresSupportingMessage)
            {
                System.Diagnostics.Debug.WriteLine(Network.Protocol.ProtocolFeature.GetFeatureName(feat));
            }*/

            /*IEnumerable<Protocol.ProtocolFeature> featuresSupportingMessageExtension = from q in _parentNode.Features.Values
                                            where q.SupportsActionOfType(message.MessageCode, Netfraction.Network.Protocol.FeatureActionType.MessageExtension)
                                            select q;*/

            //System.Diagnostics.Debug.WriteLine(featuresSupportingMessageExtension.Count());
            /*foreach (Network.Protocol.ProtocolFeature feat in featuresSupportingMessageExtension)
            {
                System.Diagnostics.Debug.WriteLine(Network.Protocol.ProtocolFeature.GetFeatureName(feat));
            }*/
            // Call ProtocolFeature handlers
            foreach (Network.Protocol.ProtocolFeature feat in featuresSupportingMessage)
            {
                feat.HandleFeatureMessage(this.mParentNode, message, featuresSupportingMessageExtension);
            }
        }

        /// <summary>
        /// Called after a message is sent to the Hub
        /// </summary>
        protected override void OnMessageSend(Network.Protocol.ProtocolMessage protocolMessage)
        {
            // Log     
            //log.Debug("[" + this.ToString() + "] Message Sent: " + protocolMessage.ToString());
        }
    }
}
